package com.jgsu.farm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jgsu.farm.entity.Analysis;
import com.jgsu.farm.entity.Information;
import com.jgsu.farm.entity.User;
import com.jgsu.farm.exception.DataAddException;
import com.jgsu.farm.exception.DataNotExistException;
import com.jgsu.farm.mapper.AnalysisMapper;
import com.jgsu.farm.mapper.InformationMapper;
import com.jgsu.farm.mapper.UserMapper;
import com.jgsu.farm.mapper.sql.UserMapperSql;
import com.jgsu.farm.service.AnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnalysisServiceImpl implements AnalysisService {
    @Autowired
    AnalysisMapper analysisMapper;
    @Autowired
    UserMapperSql userMapperSql;
    @Autowired
    InformationMapper informationMapper;


    @Override
    public void add(Analysis analysis) {
        Information informationId = informationMapper.selectByPrimaryKey(analysis.getInfoid());
        if (informationId == null) {
            throw new DataAddException("604", "插入的infoid在information表中没有，请先插入到information表");
        } else {
            if (analysis.getExpert() == null) {
                analysisMapper.insert(analysis);
            } else {
                User expertUser = userMapperSql.selectUserById(analysis.getExpert(), 2, 1);
                if (expertUser != null) {
                    analysisMapper.insert(analysis);
                }else {
                    throw new DataAddException("604", "插入的expert在information表中没有，请先插入到information表");
                }
            }
        }
    }

    @Override
    public void delete(Integer analysisid) {
        Integer i = analysisMapper.deleteByPrimaryKey(analysisid);
        if (i == 0 || i == null){
            throw new DataNotExistException("603", "不存在该id,情报分析删除失败");
        }
    }

    @Override
    public PageInfo<Analysis> selectAll(int page, int pagesize) {
        PageHelper.startPage(page, pagesize);
        List<Analysis> analysisList = analysisMapper.selectAll();
        PageInfo<Analysis> pageInfoAnalysisList = new PageInfo<Analysis>(analysisList);
        return pageInfoAnalysisList;
    }

    @Override
    public void update(Analysis analysis) {
        Analysis analysisId = analysisMapper.selectByPrimaryKey(analysis.getAnalysisid());
        Information informationId = informationMapper.selectByPrimaryKey(analysis.getInfoid());
        if (analysisId != null && informationId != null) {
            if (analysis.getExpert() == null) {
                analysisMapper.updateByPrimaryKey(analysis);
            } else {
                User expertUser = userMapperSql.selectUserById(analysis.getExpert(), 2, 1);
                if (expertUser != null) {
                    analysisMapper.updateByPrimaryKey(analysis);
                } else {
                    throw new DataNotExistException("603", "不存在该expert,情报分析更新失败");
                }
            }
        } else {
            throw new DataNotExistException("603", "不存在该analysisId,情报分析更新失败");
        }
    }


}
